import sys

from pymongo import MongoClient
from core.conf import settings
from common.log import log
from sqlalchemy import URL


def create_engine_and_session(url: str | URL):
    try:
        # 数据库引擎
        mongo_client_engine = MongoClient(url)
        log.success('MongoDB 数据库连接成功')
    except Exception as e:
        log.error('❌ MongoDB 数据库链接失败 {}', e)
        sys.exit()
    else:
        return mongo_client_engine


mongo_client_engine = create_engine_and_session(settings.MONGO_URL)


def get_client() -> MongoClient:
    return mongo_client_engine
